package com.vodden.money.shared;

import java.io.Serializable;
import java.util.Date;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable
public class Entry implements Serializable {
	/**
	 * 
	 */
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
	String id;
	@Persistent
	private Date date;
	@Persistent
	private Integer value;
	@Persistent
	private Account account;
	@Persistent
	private String description;	
	@Persistent
	private EntryGroup parent;

	private static final long serialVersionUID = 1L;

	public Entry(Date date, Account account, Integer value, String description) {
		this.setDate(date);
		this.setAccount(account);
		this.setValue(value);
		this.setDescription(description);
		this.setParent(null);
	}

	public Entry() {
		this.setDate(new Date());
		this.setValue(null);
		this.setDescription(new String());
		if (parent != null)
			this.setParent(null);
	}

	/**
	 * @return the key
	 */
	public String getId() {
		return id;
	}

	/**
	 * @param date
	 *            the date to set
	 */
	public void setDate(Date date) {
		this.date = date;
	}

	/**
	 * @return the date
	 */
	public Date getDate() {
		return date;
	}

	/**
	 * @param description
	 *            the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param value
	 *            the value to set
	 */
	public void setValue(Integer value) {
		if (parent != null)
			parent.update(this, value);
		this.value = value;
	}

	/**
	 * @return the value
	 */
	public Integer getValue() {
		return value;
	}

	/**
	 * @param account
	 *            the account to set
	 */
	public void setAccount(Account account) {
		this.account = account;
	}

	/**
	 * @return the account
	 */
	public Account getAccount() {
		return account;
	}

	/**
	 * @return the account
	 */
	public String getAccountName() {
		Account tempAccount = getAccount();
		if (tempAccount == null) {
			return "<unknown>";
		} else {
			return account.getName();
		}
	}

	/**
	 * @param parent
	 *            the parent to set
	 */
	public void setParent(EntryGroup parent) {
		this.parent = parent;
	}

	/**
	 * @return the parent
	 */
	public EntryGroup getParent() {
		return parent;
	}

}
